$(document).ready(function () {


    var licensePath = '';

    //证件 正在上传
    $('#license-upload-wrap').bind('dragover', function () {
        $('#license-upload-wrap').addClass('image-dropping');
    });
    $('#license-upload-wrap').bind('dragleave', function () {
        $('#license-upload-wrap').removeClass('image-dropping');
    });

    //如果是更换图片，先删除图片
    $('#license-upload-btn').click(function () {
        $(this).text('正在上传...');
        // var src = $('#license-upload-image').attr('src');
        if (licensePath.startsWith('http')) { //如果已经设置
            //获取图片路径
            var paras = licensePath.split('=');
            var deletePath = paras[paras.length - 1];
            //删除请求
            $.post('/image-delete', {"path": deletePath}, function (returnMsg) {
                console.log(returnMsg['msg']);
            });

        }
        $('#license-upload-input').trigger('click');
    });

    $('#license-upload-input').on('change', function () {
        console.log('--- license upload input ---');
        readLicense(this, function (formData) {
            console.log('--- formData ---', formData);
            $.post('/image-upload', formData, function (returnMsg) {

                console.log(returnMsg['src']);
                $('#license-upload-image').attr('src', returnMsg['src']);//设置图片
                licensePath = returnMsg['src']; //记录图片路径
                $('#license-upload-content').show();
                $('#license-upload-btn').text('更换图片');
            });

        });

    });

    //读取证件图片
    function readLicense(input, callback) {
        if (input.files && input.files[0]) {
            console.log('--- input --- ', input.files);

            var reader = new FileReader();
            reader.onload = function (e) {
                $('#license-upload-wrap').hide();
                var paras = {};
                paras['image']  =  e.target.result; // 图片文件
                paras['name']  =  "user";  //图片名称
                paras['format']  =  String(input.files[0].name).split('.')[1]; //统一保存为user
                // callback(formData);
                callback(paras);
            };

            reader.readAsDataURL(input.files[0]);

        } else {
            removeLicense();
        }
    }


    function removeLicense() {
        $('#license-upload-input').replaceWith($('#license-upload-input').clone());
        $('#license-upload-content').hide();
        $('#license-upload-wrap').show();
    }


    //普通用户注册
    $('#normal').ajaxForm({
        beforeSubmit: function (formData, jqForm, options) {
            console.log('--- normal signup ---', formData);
            //验证用户登录信息
            var av = new AccountValidator();
            if (av.validateForm(formData)) { //验证成功
                formData.push({name: 'role', value: 'normal'});
                return true;
            } else {
                return false;
            }
        },
        success: function (responseText, status, xhr, $form) {
            console.log('--- xhr ---', xhr);
            // console.log('--- status ---', status);
            generateNotify('', "注册成功", 'success', 2000);
            if (status == 'success')
                window.location.href = '/index#login';
        },
        error: function (e) {
            console.log('--- e ---', e.responseText);
            generateNotify('', e.responseText, 'danger', 2000);
        }
    });



    //医生/专家用户注册
    $('#doctor-expert-signup-btn').click(function () {
        console.log('--- doctor-expert-signup-btn ---');
        var av = new AccountValidator();

        var newData = av.validateDoctorExpert();
        // console.log('newData: ' + newData);

        if (newData){
            newData['role'] = 'doctor-expert';
            //
            var paras = licensePath.split('=');
            var imgPath = paras[paras.length - 1];
            newData['license'] = imgPath;

            $.post('/person-signup', newData, function (returnMsg) {
                console.log('returnMsg' + returnMsg);
                console.log('code' + returnMsg['code']);

                if (returnMsg['code'] == 1){
                    generateNotify('', "申请成功", 'success', 2000);
                    window.location.href = '/index';
                }else{
                    generateNotify('', "申请失败", 'danger', 2000);
                }
            });
        }

    });

    // $('#doctor-expert-signup').ajaxForm({
    //     beforeSubmit: function (formData, jqForm, options) {
    //         //验证用户登录信息
    //         var av = new AccountValidator();
    //         if (av.validateForm(formData)) { //验证成功
    //             if (av.validateDoctorExpert(formData)) {
    //                 formData.push({name: 'role', value: 'doctor-expert'});
    //                 //获取图片路径
    //                 //证件
    //                 if (!licensePath.startsWith('http')) {
    //                     generateNotify('', "必须上传证件", 'danger', 2000);
    //                     return false;
    //                 }
    //                 // var src = $('#license-upload-image').attr('src');
    //                 var paras = licensePath.split('=');
    //                 var imgPath = paras[paras.length - 1];
    //                 formData.push({name: 'license', value: imgPath});
    //                 console.log('--- doctor-expert signup ---', formData);
    //                 return true;
    //             }else{
    //                 return false;
    //             }
    //         }else{
    //             return false;
    //         }
    //     },
    //     success: function (responseText, status, xhr, $form) {
    //         console.log('--- status ---', status);
    //         if (status == 'success')
    //             generateNotify('', "申请成功", 'success', 2000);
    //             window.location.href = '/index#login';
    //     },
    //     error: function (e) {
    //         generateNotify('', e.responseText, 'danger', 2000);
    //     }
    // });
});
